package com.irdstudio.apicenter.gateway.util.crypt;

import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class EncryptionUtil {
	String encroptMode = "ECB";

	String paddingMode = "PKCS5Padding";

	String algorithm = "DES";

	String keyStr = "";

	String encryptDataName = "";

	String oriDataName = "";

	public String encrypt(String pwd) {
		String encryptStr="";
		try {
			BASE64Decoder base64decoder = new BASE64Decoder();
			BASE64Encoder base64encoder = new BASE64Encoder();
			byte[] keyBytes = base64decoder.decodeBuffer(this.keyStr);
			KeyGenerator generator = KeyGenerator.getInstance(this.algorithm);
			SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
			secureRandom.setSeed(keyBytes);
			generator.init(secureRandom);
			Key key = generator.generateKey();
			Cipher cipher = Cipher.getInstance(this.algorithm + "/"
					+ this.encroptMode + "/" + this.paddingMode);

			cipher.init(1, key);
			byte[] encryptBytes = cipher.doFinal(pwd.getBytes("UTF-8"));

			encryptStr = base64encoder.encode(encryptBytes);
		} catch (Exception e) {
			return encryptStr;
		}
		return encryptStr;

	}

	public void setEncroptMode(String mode) {
		this.encroptMode = mode;
	}

	public void setPaddingMode(String mode) {
		this.paddingMode = mode;
	}

	public void setAlgorithm(String alg) {
		this.algorithm = alg;
	}

	public void setKeyStr(String key) {
		this.keyStr = key;
	}

	public void setEncryptDataName(String name) {
		this.encryptDataName = name;
	}

	public void setOriDataName(String ori) {
		this.oriDataName = ori;
	}

	public static void main(String[] args) {
		EncryptionUtil jm = new EncryptionUtil();
		String pwd = "sdasdqqw";
		jm.encrypt(pwd);
	}
}
